const mongoose = require('mongoose')

const orderSchema = new mongoose.Schema({
  uid: {
    type: mongoose.Types.ObjectId,
    required: true
  },
  orderGoods: {
    type: [
      // 这里是自定义的对象数组
      // mongoose可以设置type为Object,可以直接像这样设置为{} 可以设置为Object 还可以设置为 mongoose.Types.Mixed 都是一样的1
      {
        detail: {
          type: mongoose.Types.ObjectId,
          required: true,
          // 这里是使用populate的关键点 使用哪个集合来进行填充 这里就是goods表格
          // 注意 要给detail字段传递对应的_id  Number,String,ObjectId都可以作为refs使用 但是最好使用ObjectId 除非你很猛
          ref: 'goods'
        },
        goods_num: {
          type: Number,
          required: true
        },
        zhPrice: {
          type: Number,
          required: true
        },
        enPrice: {
          type: Number,
          required: true
        },
        zhTitle: {
          type: String,
          required: true
        },
        enTitle: {
          type: String,
          required: true
        },
        img: {
          type: String,
          required: true
        }
      }
    ]
  },
  date: {
    type: Date,
    default: Date.now()
  },
  enTotal: {
    type: Number,
    required: true
  },
  zhTotal: {
    type: Number,
    required: true
  }
})
const Order = mongoose.model('orders', orderSchema)

module.exports = Order
